﻿using IRepository;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConcreteService
{
    public class AssociationService : CommonService
    {
        public Association GetRelationship(int studnetID, RelationshipFun fun)
        {
            IAssociationRepository repo = ServiceFactory.CreateInstance<IAssociationRepository>();
            return repo.GetRelationship(studnetID, fun);
        }

        public Association GetAssociationByUserID(int userID)
        {
            Association asso = null;
            UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
            AssociationService assoservice = ServiceFactory.CreateInstance<AssociationService>();
            var assorelation = utilService.ListRelationship(RelationshipFun.AssociationToStudent, userID).FirstOrDefault();
            if (assorelation != null)
            {
                asso = assoservice.Get<Association>(assorelation.NOFrom.ToString());
                asso.TypeDetail = AppConfig.GetBaseData(asso.TypeDetailNO).Description;
                asso.Type = AppConfig.GetBaseData(asso.TypeNO).Description;
                asso.Scale = AppConfig.GetBaseData(asso.ScaleNO).Description;
                asso.Nature = AppConfig.GetBaseData(asso.SchoolType.ToInt()).Description;
                asso.School = AppConfig.GetSchool(asso.SchoolNO);
                asso.Grade = AppConfig.GetBaseData(asso.AssociationGrade.ToInt()).Description;
            }
            return asso;
        }

        public Association GetAssociation(int id)
        {
            AssociationService assoservice = ServiceFactory.CreateInstance<AssociationService>();
            var asso = assoservice.Get<Association>(id.ToString());
            asso.TypeDetail = AppConfig.GetBaseData(asso.TypeDetailNO).Description;
            asso.Type = AppConfig.GetBaseData(asso.TypeNO).Description;
            asso.Scale = AppConfig.GetBaseData(asso.ScaleNO).Description;
            asso.Nature = AppConfig.GetBaseData(asso.SchoolType.ToInt()).Description;
            asso.School = AppConfig.GetSchool(asso.SchoolNO);
            asso.Grade = AppConfig.GetBaseData(asso.AssociationGrade.ToInt()).Description;

            return asso;
        }
    }
}
